package com.android.camera;

import com.android.camera.NavAnimationView.AnimatorListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class NaviAnimationController {
	private Activity mActivity;

	private Dialog mAppGuideDialog;

	private SharedPreferences naviSharedPreferences;
	private static final String NAVI_SHARED_PREFERENCE_NAME = "application_guide";
	private static final String KEY_NAVI_ANIMATION = "navi_anim_done";

	private NavAnimationView mNavAnimationView;
	private boolean isTouchOkBtn = false;

	private static final int MSG_PLAY_NAVI_ANIMATION = 1;

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case MSG_PLAY_NAVI_ANIMATION:
				if (mNavAnimationView != null) {
					mNavAnimationView.startAnimation();
				}
				break;
			}
		}
	};

	private AnimatorListener mAnimatorListener = new AnimatorListener() {

		@Override
		public void onAnimationEnd() {
			if (!isTouchOkBtn) {
				mHandler.sendEmptyMessageDelayed(MSG_PLAY_NAVI_ANIMATION, 3000);
			}
		}
	};

	public NaviAnimationController(Activity activity) {
		mActivity = activity;
	}

	public void maybeShowNaviAnimation() {
		naviSharedPreferences = mActivity.getSharedPreferences(
				NAVI_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		if (naviSharedPreferences.getBoolean(KEY_NAVI_ANIMATION, false)) {
			Log.d("sqm",
					"[maybeShowNaviAnimation]already show swipe screen guide, return");
			return;
		}

		if (mAppGuideDialog == null) {
			Log.d("sqm", "[maybeShowNaviAnimation]mAppGuideDialog == null");
			mAppGuideDialog = new AppGuideDialog(mActivity);
			mAppGuideDialog.getWindow().setLayout(
					WindowManager.LayoutParams.MATCH_PARENT,
					WindowManager.LayoutParams.MATCH_PARENT);
		}
		mAppGuideDialog.show();
	}

	public void dismissAppGuide() {
		if (mAppGuideDialog != null) {
			Log.d("sqm", "[dismissAppGuide]");
			mNavAnimationView.stopAnimation();
			mNavAnimationView = null;

			if (mHandler.hasMessages(MSG_PLAY_NAVI_ANIMATION)) {
				mHandler.removeMessages(MSG_PLAY_NAVI_ANIMATION);
			}

			mAppGuideDialog.dismiss();
			mAppGuideDialog = null;
		}
	}

	class AppGuideDialog extends Dialog {

		private Activity mActivity;
		private Button mOkBtn;

		/**
		 * ok button listner, finish app guide.
		 */
		private View.OnClickListener mOkListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("sqm", "stop navi animation.");

				naviSharedPreferences.edit()
						.putBoolean(KEY_NAVI_ANIMATION, true).commit();

				isTouchOkBtn = true;

				onBackPressed();
			}
		};

		public AppGuideDialog(Activity activity) {
			super(activity, android.R.style.Theme_Translucent_NoTitleBar);
			mActivity = activity;
		}

		@Override
		public void onBackPressed() {
			dismissAppGuide();
			super.onBackPressed();
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			LayoutInflater inflater = LayoutInflater.from(mActivity);
			View view = inflater.inflate(R.layout.navi_anim_layout, null);
			mNavAnimationView = (NavAnimationView) view
					.findViewById(R.id.nav_anim_view);
			mNavAnimationView.setAnimatorListener(mAnimatorListener);

			mOkBtn = (Button) view.findViewById(R.id.nav_ok_btn);
			mOkBtn.setOnClickListener(mOkListener);

			setContentView(view);

			mNavAnimationView.startAnimation();
		}
	}
}
